/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__list_node_mng_find_l7_proto.c                   */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/18 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <dlfcn.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frstr.h"
#include "frlist.h"
#include "frconfig.h"
#include "conntrack.h"
#include "frame_params.h"
#include "frame.h"
#include "module.h"
#include "frxml_parse.h"
#include "frmodule_stub.h"
#include "frmodule_mng.h"
#include "apmtest_cunit.h"
#include "frversion.h"

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

extern char g_arr_frame_version[30];
void __list_node_mng_free(Frlist_node *p_st_node);
int __list_node_mng_find_l7_proto(Frlist_node *p_st_node, const void *p_compare_data);

static void case1(void)
{
    Frlist_node* p_st_node = NULL;
    Frmodule_mng *p_st_module_mng = NULL;
    L7_module *p_st_l7_module = NULL;
    char *p_tmp = NULL;
    int i_len_tmp = 0;
    int i_ret = 0;

    //设置p_st_module_mng
    p_st_module_mng = (Frmodule_mng *)frmalloc(sizeof(Frmodule_mng));
    memset(p_st_module_mng,'\0',sizeof(Frmodule_mng));
    p_st_module_mng->i_is_stub = FR_YES;

    p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));
    p_st_l7_module->i_l7_proto = 1;
    p_st_l7_module->us_l4_proto = 6;
    i_len_tmp = strlen("p_module_name");
    p_tmp = (char *)frmalloc(i_len_tmp + 1);
    snprintf(p_tmp,i_len_tmp + 1,"%s","p_module_name");
    p_st_l7_module->p_module_name = p_tmp;
    i_len_tmp = strlen("p_version");
    p_tmp = (char *)frmalloc(i_len_tmp + 1);
    snprintf(p_tmp,i_len_tmp + 1,"%s","p_version");
    p_st_l7_module->p_version = p_tmp;

    p_st_node = frlist_node_create(p_st_module_mng);

    i_ret = __list_node_mng_find_l7_proto(p_st_node, p_st_l7_module);

    CU_ASSERT_EQUAL(i_ret,1);

    frfree(p_st_module_mng);
    frfree(p_st_l7_module->p_version);
    frfree(p_st_l7_module->p_module_name);
    frfree(p_st_l7_module);

    frfree(p_st_node);

    return;

}

static void case2(void)
{
    Frlist_node* p_st_node = NULL;
    int i_ret = 0;
    L7_module *p_st_l7_module = NULL;

    p_st_node = (Frlist_node *)frmalloc(sizeof(Frlist_node));
    p_st_node->data = NULL;
    p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));
    p_st_l7_module->i_l7_proto = 1;

    i_ret = __list_node_mng_find_l7_proto(p_st_node, p_st_l7_module);
    CU_ASSERT_EQUAL(i_ret,1);

    __list_node_mng_free(p_st_node);
    frfree(p_st_l7_module);
    frfree(p_st_node);
    return;

}

static void case3(void)
{
    Frlist_node* p_st_node = NULL;
    Frmodule_mng *p_st_module_mng = NULL;
    L7_module *p_st_l7_module = NULL;
    char *p_tmp = NULL;
    int i_len_tmp = 0;
    int i_ret = 0;

    //设置p_st_l7_module
    p_st_module_mng = (Frmodule_mng *)frmalloc(sizeof(Frmodule_mng));
    memset(p_st_module_mng,'\0',sizeof(Frmodule_mng));
    p_st_module_mng->i_is_stub = FR_YES;

    p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));
    p_st_l7_module->i_l7_proto = 1;
    p_st_l7_module->us_l4_proto = 6;
    i_len_tmp = strlen("p_module_name");
    p_tmp = (char *)frmalloc(i_len_tmp + 1);
    snprintf(p_tmp,i_len_tmp + 1,"%s","p_module_name");
    p_st_l7_module->p_module_name = p_tmp;
    i_len_tmp = strlen("p_version");
    p_tmp = (char *)frmalloc(i_len_tmp + 1);
    snprintf(p_tmp,i_len_tmp + 1,"%s","p_version");
    p_st_l7_module->p_version = p_tmp;

    p_st_node = frlist_node_create(p_st_module_mng);

    i_ret = __list_node_mng_find_l7_proto(p_st_node, p_st_l7_module);

    CU_ASSERT_EQUAL(i_ret,1);

    frfree(p_st_l7_module->p_version);
    frfree(p_st_l7_module->p_module_name);
    frfree(p_st_l7_module);
    __list_node_mng_free(p_st_node);
    frfree(p_st_node);

    return;

}
static void case4(void)
{
    Frlist_node* p_st_node = NULL;
    Frmodule_mng *p_st_module_mng = NULL;
    L7_module *p_st_l7_module = NULL;
    L7_module *p_st_l7_module1 = NULL;
    char *p_tmp = NULL;
    int i_len_tmp = 0;
    int i_ret = 0;

    //设置p_st_module_mng
    p_st_module_mng = (Frmodule_mng *)frmalloc(sizeof(Frmodule_mng));
    memset(p_st_module_mng,'\0',sizeof(Frmodule_mng));
    p_st_module_mng->i_is_stub = FR_YES;

    p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));
    p_st_l7_module->i_l7_proto = 1;
    p_st_l7_module->us_l4_proto = 6;
    i_len_tmp = strlen("p_module_name");
    p_tmp = (char *)frmalloc(i_len_tmp + 1);
    snprintf(p_tmp,i_len_tmp + 1,"%s","p_module_name");
    p_st_l7_module->p_module_name = p_tmp;
    i_len_tmp = strlen("p_version");
    p_tmp = (char *)frmalloc(i_len_tmp + 1);
    snprintf(p_tmp,i_len_tmp + 1,"%s","p_version");
    p_st_l7_module->p_version = p_tmp;

    p_st_node = frlist_node_create(p_st_module_mng);

    p_st_l7_module1 = (L7_module *)frmalloc(sizeof(L7_module));
    p_st_l7_module1->i_l7_proto = 2;


    i_ret = __list_node_mng_find_l7_proto(p_st_node, p_st_l7_module);

    CU_ASSERT_EQUAL(i_ret,1);

    frfree(p_st_l7_module->p_version);
    frfree(p_st_l7_module->p_module_name);
    frfree(p_st_l7_module);
    frfree(p_st_l7_module1);

    __list_node_mng_free(p_st_node);
    frfree(p_st_node);

    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__list_node_mng_find_l7_proto 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);
   	CU_add_test(pSuite, "case3", case3);
   	CU_add_test(pSuite, "case4", case4);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");

    xmlInitParser();
    frversion_set(g_arr_frame_version, sizeof(g_arr_frame_version), 0);

    run_cunit(add_suite);

    xmlCleanupParser();
    fr_slab_destroy();
    return 0;
}